package com.ihd.ihardware.view.enter.view;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.databinding.ActivityResetBinding;
import com.ihd.ihardware.pojo.LoginRes;
import com.ihd.ihardware.pojo.UserInfoRes;
import com.ihd.ihardware.view.enter.viewModel.ResetViewModel;
import com.ihd.ihardware.view.tzc.main.view.BalanceActivity;

import cn.jpush.android.api.JPushInterface;
import cn.wowjoy.commonlibrary.app.Constans;
import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.utils.AppActivityManager;
import cn.wowjoy.commonlibrary.utils.DialogUtils;
import cn.wowjoy.commonlibrary.utils.SPUtils;
import cn.wowjoy.commonlibrary.utils.ToastUtils;

public class ResetActivity extends BaseActivity<ActivityResetBinding, ResetViewModel> {

    private String phone;
    private String code;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(true).init();
        binding.mtitlebar.setTitle("忘记密码");
        binding.mtitlebar.setLeftBack(this);

        phone = getIntent().getStringExtra("phone");
        code = getIntent().getStringExtra("code");

        binding.newPswET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0 && binding.renewPswET.getText().length() > 0)
                    binding.modifyTV.setEnabled(true);
                else
                    binding.modifyTV.setEnabled(false);
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        binding.renewPswET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0 && binding.newPswET.getText().length() > 0)
                    binding.modifyTV.setEnabled(true);
                else
                    binding.modifyTV.setEnabled(false);
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        binding.modifyTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (binding.newPswET.getText().toString().equals(binding.renewPswET.getText().toString()))
                    viewModel.resetPwd(phone, binding.newPswET.getText().toString(), code);
                else
                    ToastUtils.showShort(ResetActivity.this, "两次输入密码不同");
            }
        });

        setRx(Constans.RESET, new BaseConsumer<LoginRes>() {
            @Override
            protected void onStart() {
                DialogUtils.waitingDialog(ResetActivity.this);
            }

            @Override
            protected void onSuccess(LoginRes mLiveData) {
                JsonObject mParam = new JsonObject();
                mParam.addProperty("userId", mLiveData.getData().getUserId());
                mParam.addProperty("userToken", mLiveData.getData().getUserToken());
                SPUtils.putString(Constans.GATEWAY_TOKEN, mParam.toString());
                JPushInterface.setAlias(ResetActivity.this, 0, mLiveData.getData().getUserId());
                viewModel.getUserInfo();
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(ResetActivity.this, error.getMessage());
                DialogUtils.dismiss(ResetActivity.this);
            }
        });

        setRx(AppConstans.INFO, new BaseConsumer<UserInfoRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(UserInfoRes mLiveData) {
                DialogUtils.dismiss(ResetActivity.this);
                SPUtils.putString(AppConstans.USER_INFO, new Gson().toJson(mLiveData.getData(), UserInfoRes.DataBean.class));
                Intent mIntent = new Intent(ResetActivity.this, BalanceActivity.class);
                startActivity(mIntent);
                AppActivityManager.getInstance().killActivity(PerActivity.class);
                finish();
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(ResetActivity.this, error.getMessage());
                DialogUtils.dismiss(ResetActivity.this);
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_reset;
    }

    @Override
    protected Class getViewModelClass() {
        return ResetViewModel.class;
    }
}
